//
//  CalendarVC.m
//  ProjectSource_Demo
//
//  Created by Liu on 16/3/19.
//  Copyright © 2016年 AngryBear. All rights reserved.
//

#import "CalendarVC.h"

#import "CalendarView.h"
#import "WorkingCalendarCell.h"

#define CALENDAR_HEIGHT 190

@interface CalendarVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIImageView *calendarHeaderView;
@property (nonatomic, strong) CalendarView *calendarView;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation CalendarVC

- (void)dealloc
{
    [self removeObserver:self.calendarView forKeyPath:@"stringOfSlectedDate" context:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if ([UIDevice currentDevice].systemVersion.floatValue >= 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    self.view.backgroundColor = [UIColor colorWithHexRGB:0xefefef];
    [self.view addSubview:self.calendarHeaderView];
    [self.view addSubview:self.tableView];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, CALENDAR_HEIGHT)];
    [view addSubview:self.calendarView];
    self.tableView.tableHeaderView = view;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, CALENDAR_HEIGHT + self.calendarHeaderView.height)];
    
    self.calendarView.selectedDate = [NSDate date];
    
    [self addObserver:self.calendarView forKeyPath:@"stringOfSlectedDate" options:NSKeyValueObservingOptionNew context:nil];
}

#pragma mark - KVO Handel
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    self.title = self.calendarView.stringOfSlectedDate;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WorkingCalendarCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WorkingCalendarCell class])];
    
    cell.typeImageView.backgroundColor = [UIColor redColor];
    cell.typeLabel.text = @"询价工作";
    cell.titleLabel.text = @"2016年办公室用品年度询价";
    cell.deadlineLabel.text = @"截止时间：";
    cell.dateLabel.text = @"2016-03-26  18:00";
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 91 + 6;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = cell.selected;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        cell.selected = cell.selected;
    });
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat topOffset = self.calendarView.selectedItemRect.origin.y;
    CGFloat offsetY = self.tableView.contentOffset.y + self.calendarHeaderView.height;
    CGFloat height = CALENDAR_HEIGHT - self.calendarView.selectedItemRect.size.height;

    if (offsetY >= topOffset) {
        if (offsetY >= height) {
            if (self.calendarView.superview != self.view) {
                [self.view addSubview:self.calendarView];
                [self.view bringSubviewToFront:self.calendarHeaderView];
            }
            self.calendarView.top = -topOffset + self.calendarHeaderView.height;
            self.calendarView.height = self.calendarView.selectedItemRect.origin.y + self.calendarView.selectedItemRect.size.height;
        }
        else {
            self.calendarView.top = offsetY - topOffset;
            self.calendarView.height = CALENDAR_HEIGHT - offsetY + topOffset;
            
            if (self.calendarView.superview != self.tableView.tableHeaderView) {
                [self.tableView.tableHeaderView addSubview:self.calendarView];
            }
        }
    }
    else {
        self.calendarView.top = 0;
        self.calendarView.height = CALENDAR_HEIGHT;
        
        if (self.calendarView.superview != self.tableView.tableHeaderView) {
            [self.tableView.tableHeaderView addSubview:self.calendarView];
        }
    }
    
    if (offsetY < 0) {
        self.calendarHeaderView.top = -self.calendarHeaderView.height;
        [self.calendarView addSubview:self.calendarHeaderView];
    }
    else {
        self.calendarHeaderView.top = 0;
        [self.view addSubview:self.calendarHeaderView];
    }
}

#pragma mark - Getter Methods
- (CalendarView *)calendarView
{
    if (_calendarView == nil) {
        _calendarView = [[CalendarView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CALENDAR_HEIGHT)];
        [_calendarView setBackgroundImage:[UIImage imageNamed:@"calendar_bg"]];
    }
    return _calendarView;
}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.contentInset = UIEdgeInsetsMake(self.calendarHeaderView.height, 0, 0, 0);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [_tableView registerClass:[WorkingCalendarCell class] forCellReuseIdentifier:NSStringFromClass([WorkingCalendarCell class])];
    }
    return _tableView;
}

- (UIImageView *)calendarHeaderView
{
    if (_calendarHeaderView == nil) {
        _calendarHeaderView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 24)];
        _calendarHeaderView.image = [UIImage imageNamed:@"calendar_week"];
        
        CGFloat space = (self.view.width - 7*10)/8;
        NSArray *titles = @[@"一", @"二", @"三", @"四", @"五", @"六", @"日"];
        for (int i = 0; i < 7; i++) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(space + (space + 10)*i, 7, 10, 10)];
            label.backgroundColor = [UIColor clearColor];
            label.textColor = [UIColor colorWithHexRGB:0xb2b2b2];
            label.font = [UIFont systemFontOfSize:10];
            label.textAlignment = NSTextAlignmentCenter;
            [_calendarHeaderView addSubview:label];
            label.text = [titles objectAtIndex:i];
        }
    }
    return _calendarHeaderView;
}

@end
